<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2021/9/24
  Time: 13:52
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>用户注册</title>
    <script src="js/jquery-3.6.0.js"></script>
</head>
<body>
<form action="reg" method="post">
    姓名:<input type="text" name="username"><span id="error_name"></span> <br>
         <input type="submit" value="注册">
</form>
<script>
    //失去焦点事件
    $("[name='username']").blur(function () {
        var  username=$("[name='username']").val();
        console.log(username);
        if (username==null||username.trim()==""){
            $("#error_name").text("用户名不能为空");//设置提示信息
            $("#error_name").css("color","red");//改变错误字体颜色
            return;//结束执行
        }
        $.ajax({
            url:"verify",
            data:{name:username,date:new Date()},
            type:"post",
            dataType:"text",
            async:true,
            success:function (msg) {//如果数据库存在该用户名，返回true，否则返回false
                console.log(msg);
                if (msg=="true"){
                    $("#error_name").text("该用户名已经被占用");
                    $("#error_name").css("color","red");
                }else {
                    $("#error_name").text("该用户名可用");
                    $("#error_name").css("color","green");
                }
            },
            error:function (err) {
                console.log(err)
            }
        })
    })
</script>
</body>
</html>
